from django.db import models
from user.models import SysUser
import uuid

# Create your models here.

class PosterRecord(models.Model):
    """
    海报生成记录模型类，用于记录用户的海报生成历史。

    属性:
    - id: 自动增长的主键
    - user: 关联的用户（外键到SysUser）
    - image_url: 生成的海报图片地址
    - prompt: 生成海报使用的提示词
    - poster_type: 海报操作类型（生成、修改、清晰化、添加元素）
    - create_time: 生成时间
    - update_time: 更新时间
    - is_deleted: 软删除标记
    - remark: 备注信息
    """
    
    # 海报类型选择
    POSTER_TYPE_CHOICES = [
        ('generate', '生成海报'),
        ('modify', '修改海报'),
        ('enhance', '清晰化'),
        ('add_elements', '添加Logo和二维码'),
    ]
    
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(
        SysUser, 
        on_delete=models.CASCADE, 
        verbose_name="用户",
        db_comment="关联的用户ID",
        related_name='poster_records'
    )
    image_url = models.URLField(
        max_length=500, 
        verbose_name="图片地址",
        db_comment="生成的海报图片URL地址"
    )
    prompt = models.TextField(
        max_length=2000, 
        blank=True, 
        null=True, 
        verbose_name="提示词",
        db_comment="生成海报使用的提示词描述"
    )
    poster_type = models.CharField(
        max_length=20, 
        choices=POSTER_TYPE_CHOICES, 
        default='generate',
        verbose_name="海报类型",
        db_comment="海报操作类型：生成/修改/清晰化/添加元素"
    )
    create_time = models.DateTimeField(
        auto_now_add=True, 
        verbose_name="生成时间",
        db_comment="海报生成的时间"
    )
    update_time = models.DateTimeField(
        auto_now=True, 
        verbose_name="更新时间",
        db_comment="记录最后更新时间"
    )
    is_deleted = models.BooleanField(
        default=False, 
        verbose_name="是否删除",
        db_comment="软删除标记，0正常1删除"
    )
    remark = models.CharField(
        max_length=500, 
        null=True, 
        blank=True, 
        verbose_name="备注",
        db_comment="额外备注信息"
    )

    class Meta:
        db_table = "poster_records"
        verbose_name = "海报生成记录"
        verbose_name_plural = "海报生成记录"
        ordering = ['-create_time']  # 按创建时间倒序排列
        
    def __str__(self):
        return f"{self.user.username} - {self.get_poster_type_display()} - {self.create_time.strftime('%Y-%m-%d %H:%M')}"


class ChatSession(models.Model):
    """
    聊天会话模型，用于管理用户的多轮对话会话
    """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = models.ForeignKey(
        SysUser,
        on_delete=models.CASCADE,
        verbose_name="用户",
        db_comment="关联的用户ID",
        related_name='chat_sessions'
    )
    title = models.CharField(
        max_length=200,
        verbose_name="会话标题",
        db_comment="会话的显示标题",
        default="新对话"
    )
    create_time = models.DateTimeField(
        auto_now_add=True, 
        verbose_name="创建时间",
        db_comment="会话创建时间"
    )
    update_time = models.DateTimeField(
        auto_now=True, 
        verbose_name="更新时间",
        db_comment="会话最后更新时间"
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name="是否活跃",
        db_comment="会话是否活跃，0删除1正常"
    )

    class Meta:
        db_table = "chat_sessions"
        verbose_name = "聊天会话"
        verbose_name_plural = "聊天会话"
        ordering = ['-update_time']

    def __str__(self):
        return f"{self.user.username} - {self.title}"


class ChatMessage(models.Model):
    """
    聊天消息模型，用于存储对话中的每条消息
    """
    MESSAGE_TYPE_CHOICES = [
        ('user', '用户消息'),
        ('assistant', '助手回复'),
    ]

    id = models.AutoField(primary_key=True)
    session = models.ForeignKey(
        ChatSession,
        on_delete=models.CASCADE,
        verbose_name="所属会话",
        db_comment="关联的会话ID",
        related_name='messages'
    )
    message_type = models.CharField(
        max_length=20,
        choices=MESSAGE_TYPE_CHOICES,
        verbose_name="消息类型",
        db_comment="消息类型：用户消息或助手回复"
    )
    content = models.TextField(
        verbose_name="消息内容",
        db_comment="消息的具体内容"
    )
    create_time = models.DateTimeField(
        auto_now_add=True, 
        verbose_name="发送时间",
        db_comment="消息发送时间"
    )

    class Meta:
        db_table = "chat_messages"
        verbose_name = "聊天消息"
        verbose_name_plural = "聊天消息"
        ordering = ['create_time']

    def __str__(self):
        return f"{self.session.title} - {self.get_message_type_display()}: {self.content[:50]}"
